package com.woniu40.petai_manager.web;

import com.github.pagehelper.PageInfo;

import com.woniu40.petai_manager.entity.Permission;
import com.woniu40.petai_manager.result.ResponseResult;
import com.woniu40.petai_manager.service.PermissionService;
import com.woniu40.petai_manager.vo.PermissionVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    PermissionService permissionService;

    @GetMapping("list")
    @ApiOperation("查询所有权限")
    public ResponseResult findAll(){
        List<PermissionVo> permissionVoList = permissionService.findAllPermissionVo();
        ResponseResult<List<PermissionVo>> result = new ResponseResult<>();
        if(permissionVoList.size()>0){
            result.setCode(200);
            result.setMessage("查询成功");
            result.setData(permissionVoList);
        }else {
            result.setCode(107);
            result.setMessage("查询失败");
        }
        return result;
    }

    @GetMapping("/findAllPermission")
    @ApiOperation("查询所有权限")
    public ResponseResult<PageInfo<Permission>> findAllPermission(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize,String name){
        ResponseResult<PageInfo<Permission>> result = new ResponseResult<>();
        PageInfo<Permission> allPermission = permissionService.findAllPermission(pageNum, pageSize,name);
        result.setCode(200);
        result.setMessage("查询成功");
        result.setData(allPermission);
        return result;
    }

    @GetMapping("/findParent")
    @ApiOperation("查询所有权限上级菜单")
    public ResponseResult<List<Permission>> findParent(){
        List<Permission> parent = permissionService.findParent();
        ResponseResult<List<Permission>> result = new ResponseResult<>();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setData(parent);
        return result;
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除权限")
    @ApiImplicitParam(name = "id",required = true,dataType = "Integer")
    public ResponseResult<String> delete(Integer id){
        int delete = permissionService.delete(id);
        ResponseResult<String> result = new ResponseResult<>();
        if(delete!=0){
            result.setCode(200);
            result.setMessage("删除成功");
        }else {
            result.setCode(109);
            result.setMessage("删除失败");
        }
        return result;
    }

    @PostMapping("/add")
    @ApiOperation("/添加权限")
    @ApiImplicitParam(name = "permission",required = true,dataType = "Permission")
    public ResponseResult<String> add(@RequestBody Permission permission){
        System.out.println(permission);
        int i = permissionService.addPermission(permission);
        ResponseResult<String> result = new ResponseResult<>();
        if(i!=0){
            result.setCode(200);
            result.setMessage("添加成功");
        }else {
            result.setCode(110);
            result.setMessage("添加失败");
        }
        return result;
    }

    @PutMapping("/update")
    @ApiOperation("更新权限")
    @ApiImplicitParam(name = "permission",required = true,dataType = "Permission")
    public ResponseResult<String> update(@RequestBody Permission permission){
        System.out.println(permission);
        int i = permissionService.updateByPrimaryKey(permission);
        ResponseResult<String> result = new ResponseResult<>();
        if(i!=0){
            result.setCode(200);
            result.setMessage("更新成功");
        }else {
            result.setCode(108);
            result.setMessage("更新失败");
        }
        return result;
    }







}
